<template>
  <div>
    <el-form label-width="120px" style="text-align: left">
      <el-form-item label="课程标题">
        <el-input v-model="courseInfo.title" placeholder="示例：机器学习项目课：从基础到搭建项目"/>
      </el-form-item>

      <!--所属分类 -->
      <el-form-item label="课程分类">
        <el-cascader
          :options="subjectList"
          :props="{checkStrictly: true,value: 'id',label: 'label',children: 'children'}"
          v-model="cascaderSubject"
          @change="handleSubjectChange"></el-cascader>
      </el-form-item>

      <!--课程讲师-->
      <el-form-item label="课程讲师">
        <el-select v-model="courseInfo.teacherId" placeholder="请选择">
          <el-option v-for="teacher in teacherList" :key="teacher.id" :label="teacher.name" :value="teacher.id"/>
        </el-select>
      </el-form-item>

      <el-form-item label="总课时">
        <el-input-number :min="0" v-model="courseInfo.lessonNum" controls-position="right" placeholder="请填写总课时数"/>
      </el-form-item>

      <!--课程简介-->
      <el-form-item label="简介">
         <my-quill-editor placeholder="请输入简介" v-model="courseInfo.description"/>
      </el-form-item>

      <!--课程封面-->
      <el-form-item label="课程封面">
        <el-upload
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
          :action="BASE_API+'/oss/uploadFile'"
          class="avatar-uploader">
          <img v-if="courseInfo.cover" :src="courseInfo.cover" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>

      <el-form-item label="课程价格">
        <el-input-number :min="0" v-model="courseInfo.price" controls-position="right" placeholder="请填写总课时数"/>
      </el-form-item>
    </el-form>

    <el-form style="text-align: center">
      <el-form-item>
        <el-button type="primary" @click="next">保存并下一步</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
    import http from "@/api/course"
    import httpTeacher from "@/api/teacher"
    import httpSubject from "@/api/subject"
    import MyQuillEditor from "../../../components/QuillEditor/index";

    export default {
        name: "info",
        components: {MyQuillEditor},
        props:{
            globalCourseInfo:{}
        },
        data() {
            return {
                courseInfo: {
                    title: "",
                    subjectId: '',
                    teacherId: '',
                    lessonNum: 0,
                    description: '',
                    cover: '',
                    price: 0,
                },
                teacherList: [],
                subjectList: [],
                BASE_API: process.env.BASE_API,
                cascaderSubject: ''
            }
        },
        methods: {
            next() {
                http.addOrUpdateCourseInfo(this.courseInfo).then(res => {
                    this.$message({
                        type: "success",
                        message: "课程基本信息填写成功"
                    })
                    this.$emit("courseInfo",res.data)
                    this.$emit("next")
                })
            },
            getListTeacher() {
                httpTeacher.getTeacherList(0, 1000, null).then(res => {
                    this.teacherList = res.data.records
                })
            },
            getSubjectList() {
                httpSubject.getSubjectList().then(res => {
                    this.subjectList = res.data;
                });
            },
            handleSubjectChange(val) {
                if (val){
                    this.courseInfo.subjectId = val[val.length - 1]
                }
            },
            handleAvatarSuccess(res,file){
                this.courseInfo.cover = this.BASE_API+res.data
            },
            beforeAvatarUpload(file){
                //TODO 封面上传约束
            },
            init(){ //刚开始不会触发，包括更新课程，只有返回上一步时 this.globalCourseInfo.id有数据才触发
                if (this.globalCourseInfo.id){
                    this.courseInfo = this.globalCourseInfo
                }
            }
        },
        watch: {
            //为了解决数据回显的bug，
            //课程修改时需要查询异步课程但是此子组件已经被创建，应该监听globalCourseInfo来改变courseInfo
            globalCourseInfo:{
                handler(val,oldVal){
                    this.courseInfo = val
                },
                deep: true
            },
            courseInfo:{
                handler(val,oldVal){
                  if (val.subjectId!==oldVal.subjectId){
                      this.cascaderSubject = val.subjectId
                  }
                },
                deep:true
            }
        },
        created() {
            this.init()
            this.getListTeacher()
            this.getSubjectList()
        },
    }
</script>

<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
